//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.config.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.ht.modules.system.online.config.service.DbTableHandleI;
import com.ht.modules.system.online.config.util.ColumnMeta;

public class DbTableMysqlHandleImpl implements DbTableHandleI {
    public DbTableMysqlHandleImpl() {
    }

    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD COLUMN " + this.getAddFieldDesc(columnMeta) + ";";
    }

    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "CHANGE COLUMN " + columnMeta.getOldColumnName() + " " + this.getRenameFieldDesc(columnMeta) + " ;";
    }

    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " MODIFY COLUMN " + this.getUpdateFieldDesc(cgformcolumnMeta, datacolumnMeta) + ";";
    }

    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String result = "";
        if (dataType.equalsIgnoreCase("varchar")) {
            result = "string";
        } else if (dataType.equalsIgnoreCase("double")) {
            result = "double";
        } else if (dataType.equalsIgnoreCase("int")) {
            result = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("Datetime")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("decimal")) {
            result = "bigdecimal";
        } else if (dataType.equalsIgnoreCase("text")) {
            result = "text";
        } else if (dataType.equalsIgnoreCase("blob")) {
            result = "blob";
        }

        return result;
    }

    public String dropTableSQL(String tableName) {
        return " DROP TABLE IF EXISTS " + tableName + " ;";
    }

    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }

    private String getFieldDesc(ColumnMeta cgfromcolumnMeta, ColumnMeta datacolumnMeta) {
        String result = "";
        if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = cgfromcolumnMeta.getColumnName() + " varchar(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = cgfromcolumnMeta.getColumnName() + " datetime " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = cgfromcolumnMeta.getColumnName() + " int(" + cgfromcolumnMeta.getColumnSize() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = cgfromcolumnMeta.getColumnName() + " double(" + cgfromcolumnMeta.getColumnSize() + "," + cgfromcolumnMeta.getDecimalDigits() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("bigdecimal")) {
            result = cgfromcolumnMeta.getColumnName() + " decimal(" + cgfromcolumnMeta.getColumnSize() + "," + cgfromcolumnMeta.getDecimalDigits() + ") " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("text")) {
            result = cgfromcolumnMeta.getColumnName() + " text " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (cgfromcolumnMeta.getColunmType().equalsIgnoreCase("blob")) {
            result = cgfromcolumnMeta.getColumnName() + " blob " + ("Y".equals(cgfromcolumnMeta.getIsNullable()) ? "NULL" : "NOT NULL");
        }

        result = result + (StringUtils.isNotEmpty(cgfromcolumnMeta.getComment()) ? " COMMENT '" + cgfromcolumnMeta.getComment() + "'" : " ");
        result = result + (StringUtils.isNotEmpty(cgfromcolumnMeta.getFieldDefault()) ? " DEFAULT " + cgfromcolumnMeta.getFieldDefault() : " ");
        String pkType = cgfromcolumnMeta.getPkType();
        if ("id".equalsIgnoreCase(cgfromcolumnMeta.getColumnName()) && pkType != null && ("SEQUENCE".equalsIgnoreCase(pkType) || "NATIVE".equalsIgnoreCase(pkType))) {
            result = result + " AUTO_INCREMENT ";
        }

        return result;
    }

    private String getUpdateFieldDesc(ColumnMeta cgfromcolumnMeta, ColumnMeta datacolumnMeta) {
        String result = this.getFieldDesc(cgfromcolumnMeta, datacolumnMeta);
        return result;
    }

    private String getAddFieldDesc(ColumnMeta cgfromcolumnMeta) {
        String result = this.getFieldDesc(cgfromcolumnMeta, (ColumnMeta)null);
        return result;
    }

    private String getRenameFieldDesc(ColumnMeta cgfromcolumnMeta) {
        String result = this.getFieldDesc(cgfromcolumnMeta, (ColumnMeta)null);
        return result;
    }

    public String getCommentSql(ColumnMeta columnMeta) {
        return "";
    }

    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName + " ON " + tableName;
    }
}
